package com.sfac.javaSpringBoot.modules.account.controller;

import com.github.pagehelper.PageInfo;
import com.sfac.javaSpringBoot.modules.account.entity.User;
import com.sfac.javaSpringBoot.modules.account.service.UserService;
import com.sfac.javaSpringBoot.modules.common.entity.Result;
import com.sfac.javaSpringBoot.modules.common.entity.Search;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api")
public class UserController {

    @Autowired
    private UserService userService;

    /**
     * 127.0.0.1/api/user ---- post
     * {"userName":"aaa","password":"123456"}
     */
    @PostMapping(value = "/user", consumes = "application/json")
    public Result<User> insertUser(@RequestBody User user)
    {
        return userService.insertUser(user);
    }

    /**
     * 127.0.0.1/api/user ---- put
     * {"id":4,"userName":"aaa","password":"123456"}
     */

    @PutMapping(value = "/user", consumes = "application/json")
    public Result<User> updateUser(@RequestBody User user) {

        return userService.updateUser(user);
    }

    /**
     * 127.0.0.1/api/user/1 ---- get
     */
    @GetMapping("/user/{id}")
    public User getUserById(@PathVariable int id) {
        return userService.getUserById(id);
    }

    /**
     * 127.0.0.1/api/user/1 ---- delete
     */
    @DeleteMapping("/user/{id}")
    public Result<Object> deleteUserById(@PathVariable int id) {

        return userService.deleteUserById(id);
    }

    /**
     * 127.0.0.1/api/users ---- post                           ----------- 分页 ---------------
     * {"currentPage":1,"pageSize":5,"orderBy":"id","direction":"asc","keyword":""}
     */
    @PostMapping(value = "/users", consumes = "application/json")
    public PageInfo<User> getUsersBySearch(@RequestBody Search search) {

        return userService.getUsersBySearch(search);
    }

    /**
     * 127.0.0.1/api/login ---- post
     * {"userName":"aaa","password":"123456"}
     */
    @PostMapping(value = "/login", consumes = "application/json")
    public Result<User> login (@RequestBody User user) {
        return userService.getUserByUserNameAndPassword(
                user.getUserName(), user.getPassword());
    }
}